home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / gfx / misc / gnuplot-src.lha / gnuplot-3.7.1src / gnuplot-3.7.1.lha / gnuplot-3.7.1 / win / winmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-21  |  11.3 KB  |  490 lines

  1. #ifndef lint…¢PÿÿSid = "$Id: winmain.c,v 1.3.2.1 1999/09/21 18:48:28 lhecking Exp $";
  2. #endif
  3.  
  4. /* GNUPLOT - win/winmain.c */
  5. /*[
  6.  * Copyright 1992, 1993, 1998   Maurice Castro, Russell Lang
  7.  *
  8.  * Permission to use, copy, and distribute this software and its
  9.  * documentation for any purpose with or without fee is hereby granted,
  10.  * provided that the above copyright notice appear in all copies and
  11.  * that both that copyright notice and this permission notice appear
  12.  * in supporting documentation.
  13.  *
  14.  * Permission to modify the software is granted, but not the right to
  15.  * distribute the complete modified source code.  Modifications are to
  16.  * be distributed as patches to the released version.  Permission to
  17.  * distribute binaries produced by compiling modified sources is granted,
  18.  * provided you
  19.  *   1. distribute the corresponding source modifications from the
  20.  *    released version in the form of a patch file along with the binaries,
  21.  *   2. add special version identification to distinguish your version
  22.  *    in addition to the base release version number,
  23.  *   3. provide your name and address as the primary contact for the
  24.  *    support of your modified version, and
  25.  *   4. retain our contact information in regard to use of the base
  26.  *    software.
  27.  * Permission to distribute the released version of the source code along
  28.  * with corresponding source modifications in the form of a patch file is
  29.  * granted with same provisions 2 through 4 for binary distributions.
  30.  *
  31.  * This software is provided "as is" without express or implied warranty
  32.  * to the extent permitted by applicable law.
  33. ]*/
  34.  
  35. /*
  36.  * AUTHORS
  37.  * 
  38.  *   Maurice Castro
  39.  *   Russell Lang
  40.  * 
  41.  * Send your comments or suggestions to 
  42.  *  info-gnuplot@dartmouth.edu.
  43.  * This is a mailing list; to join it send a note to 
  44.  *  majordomo@dartmouth.edu.  
  45.  * Send bug reports to
  46.  *  bug-gnuplot@dartmouth.edu.
  47.  */
  48.  
  49. /* This file implements the initialization code for running gnuplot   */
  50. /* under Microsoft Windows. The code currently compiles only with the */
  51. /* Borland C++ 3.1 compiler. 
  52. /*                                                                    */
  53. /* The modifications to allow Gnuplot to run under Windows were made  */
  54. /* by Maurice Castro. (maurice@bruce.cs.monash.edu.au)  3 Jul 1992    */
  55. /* and Russell Lang (rjl@monu1.cc.monash.edu.au) 30 Nov 1992          */
  56. /*                                                                    */
  57.  
  58. #define STRICT
  59. #include <windows.h>
  60. #include <windowsx.h>
  61. #include <dos.h>
  62. #include <stdio.h>
  63. #include <stdlib.h>
  64. #include <string.h>
  65. #include <stdarg.h>
  66. #include <ctype.h>
  67. #ifdef __MSC__
  68. #include <malloc.h>
  69. #else
  70. # ifdef __TURBOC__ /* HBB 981201: MinGW32 doesn't have this */
  71. #include <alloc.h>
  72. #endif
  73. #endif
  74. #include <io.h>
  75. #include "plot.h"
  76. #include "setshow.h"
  77. #include "wgnuplib.h"
  78. #include "wtext.h"
  79.  
  80. /* limits */
  81. #define MAXSTR 255
  82. #define MAXPRINTF 1024
  83.  
  84. /* globals */
  85. TW textwin;
  86. GW graphwin;
  87. PW pausewin;
  88. MW menuwin;
  89. LPSTR szModuleName;
  90. LPSTR winhelpname;
  91. LPSTR szMenuName;
  92. #define MENUNAME "wgnuplot.mnu"
  93. #ifndef HELPFILE /* HBB 981203: makefile.win predefines this... */
  94. #define HELPFILE "wgnuplot.hlp"
  95. #endif
  96.  
  97. extern char version[];
  98. extern char patchlevel[];
  99. extern char date[];
  100. /*extern char *authors[];*/
  101. char *authors[]={
  102.                  "Colin Kelly",
  103.                  "Thomas Williams"
  104.                 };
  105.  
  106. extern char gnuplot_copyright[];
  107. void WinExit(void);
  108. int gnu_main(int argc, char *argv[], char *env[]);
  109.  
  110. void
  111. CheckMemory(LPSTR str)
  112. {
  113.     if (str == (LPSTR)NULL) {
  114.         MessageBox(NULL, "out of memory", "gnuplot", MB_ICONSTOP | MB_OK);
  115.         exit(1);
  116.     }
  117. }
  118.  
  119. int
  120. Pause(LPSTR str)
  121. {
  122.     pausewin.Message = str;
  123.     return (PauseBox(&pausewin) == IDOK);
  124. }
  125.  
  126. /* atexit procedure */
  127. void
  128. WinExit(void)
  129. {
  130.     term_reset();
  131.  
  132. #ifndef __MINGW32__ /* HBB 980809: FIXME: doesn't exist for MinGW32. So...? */
  133.     fcloseall();
  134. #endif
  135.     if (graphwin.hWndGraph && IsWindow(graphwin.hWndGraph))
  136.         GraphClose(&graphwin);
  137.     TextMessage();    /* process messages */
  138.      WinHelp(textwin.hWndText,(LPSTR)winhelpname,HELP_QUIT,(DWORD)NULL);
  139.     TextClose(&textwin);
  140.     TextMessage();    /* process messages */
  141.     return;
  142. }
  143.  
  144. /* call back function from Text Window WM_CLOSE */
  145. int CALLBACK WINEXPORT
  146. ShutDown(void)
  147. {
  148. #if 0  /* HBB 19990505: try to avoid crash on clicking 'close' */
  149.        /* Problem was that WinExit was called *twice*, once directly,
  150.         * and again via 'atexit'. This caused problems by double-freeing
  151.         * of GlobalAlloc-ed memory inside TextClose() */
  152.        /* Caveat: relies on atexit() working properly */
  153.     WinExit();
  154. #endif
  155.     exit(0);
  156.     return 0;
  157. }
  158.  
  159. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  160.         LPSTR lpszCmdLine, int nCmdShow)
  161. {
  162.     /*WNDCLASS wndclass;*/
  163.     LPSTR tail;
  164.     
  165. #ifdef __MSC__  /* MSC doesn't give us _argc and _argv[] so ...   */
  166. #define MAXCMDTOKENS 128
  167.     int     _argc=0;
  168.     LPSTR   _argv[MAXCMDTOKENS];
  169.     _argv[_argc] = "wgnuplot.exe";
  170.     _argv[++_argc] = _fstrtok( lpszCmdLine, " ");
  171.     while (_argv[_argc] != NULL)
  172.         _argv[++_argc] = _fstrtok( NULL, " ");
  173. #endif /* __MSC__ */
  174.  
  175.     szModuleName = (LPSTR)farmalloc(MAXSTR+1);
  176.     CheckMemory(szModuleName);
  177.  
  178.     /* get path to EXE */
  179.     GetModuleFileName(hInstance, (LPSTR) szModuleName, MAXSTR);
  180. #ifndef WIN32
  181.     if (CheckWGNUPLOTVersion(WGNUPLOTVERSION)) {
  182.         MessageBox(NULL, "Wrong version of WGNUPLOT.DLL", szModuleName, MB_ICONSTOP | MB_OK);
  183.         exit(1);
  184.     }
  185. #endif
  186.     if ((tail = (LPSTR)_fstrrchr(szModuleName,'\\')) != (LPSTR)NULL)
  187.     {
  188.         tail++;
  189.         *tail = 0; 
  190.     }
  191.     szModuleName = (LPSTR)farrealloc(szModuleName, _fstrlen(szModuleName)+1);
  192.     CheckMemory(szModuleName);
  193.  
  194.     winhelpname = (LPSTR)farmalloc(_fstrlen(szModuleName)+_fstrlen(HELPFILE)+1);
  195.     CheckMemory(winhelpname);
  196.     _fstrcpy(winhelpname,szModuleName);
  197.     _fstrcat(winhelpname,HELPFILE);
  198.  
  199.     szMenuName = (LPSTR)farmalloc(_fstrlen(szModuleName)+_fstrlen(MENUNAME)+1);
  200.     CheckMemory(szMenuName);
  201.     _fstrcpy(szMenuName,szModuleName);
  202.     _fstrcat(szMenuName,MENUNAME);
  203.  
  204.     textwin.hInstance = hInstance;
  205.     textwin.hPrevInstance = hPrevInstance;
  206.     textwin.nCmdShow = nCmdShow;
  207.     textwin.Title = "gnuplot";
  208.     textwin.IniFile = "wgnuplot.ini";
  209.     textwin.IniSection = "WGNUPLOT";
  210.     textwin.DragPre = "load '";
  211.     textwin.DragPost = "'\n";
  212.     textwin.lpmw = &menuwin;
  213.     textwin.ScreenSize.x = 80;
  214.     textwin.ScreenSize.y = 80;
  215.     textwin.KeyBufSize = 2048;
  216.     textwin.CursorFlag = 1;    /* scroll to cursor after \n & \r */
  217.     textwin.shutdown = MakeProcInstance((FARPROC)ShutDown, hInstance);
  218.     textwin.AboutText = (LPSTR)farmalloc(1024);
  219.     CheckMemory(textwin.AboutText);
  220.     sprintf(textwin.AboutText,"Version %s\nPatchlevel %s\nLast Modified %s\n%s\n%s, %s and many others",
  221.         version, patchlevel, date, gnuplot_copyright, authors[1], authors[0]);
  222.     textwin.AboutText = (LPSTR)farrealloc(textwin.AboutText, _fstrlen(textwin.AboutText)+1);
  223.     CheckMemory(textwin.AboutText);
  224.  
  225.     menuwin.szMenuName = szMenuName;
  226.  
  227.     pausewin.hInstance = hInstance;
  228.     pausewin.hPrevInstance = hPrevInstance;
  229.     pausewin.Title = "gnuplot pause";
  230.  
  231.     graphwin.hInstance = hInstance;
  232.     graphwin.hPrevInstance = hPrevInstance;
  233.     graphwin.Title = "gnuplot graph";
  234.     graphwin.lptw = &textwin;
  235.     graphwin.IniFile = textwin.IniFile;
  236.     graphwin.IniSection = textwin.IniSection;
  237.     graphwin.color=TRUE;
  238.     graphwin.fontsize = WINFONTSIZE;
  239.  
  240.     if (TextInit(&textwin))
  241.         exit(1);
  242.     textwin.hIcon = LoadIcon(hInstance, "TEXTICON");
  243. #ifdef WIN32
  244.     SetClassLong(textwin.hWndParent, GCL_HICON, (DWORD)textwin.hIcon);
  245. #else
  246.     SetClassWord(textwin.hWndParent, GCW_HICON, (WORD)textwin.hIcon);
  247. #endif
  248.     if (_argc>1) {
  249.         int i,noend=FALSE;
  250.         for (i=0; i<_argc; ++i)
  251.             if (!stricmp(_argv[i],"-noend") || !stricmp(_argv[i],"/noend"))
  252.                 noend = TRUE;
  253.         if (noend)
  254.             ShowWindow(textwin.hWndParent, textwin.nCmdShow);
  255.     }
  256.     else
  257.         ShowWindow(textwin.hWndParent, textwin.nCmdShow);
  258.     if (IsIconic(textwin.hWndParent)) { /* update icon */
  259.         RECT rect;
  260.         GetClientRect(textwin.hWndParent, (LPRECT) &rect);
  261.         InvalidateRect(textwin.hWndParent, (LPRECT) &rect, 1);
  262.         UpdateWindow(textwin.hWndParent);
  263.     }
  264.  
  265.  
  266.     atexit(WinExit);
  267.  
  268.     gnu_main(_argc, _argv, environ);
  269.  
  270.     return 0;
  271. }
  272.  
  273.  
  274. /* replacement stdio routines that use Text Window for stdin/stdout */
  275. /* WARNING: Do not write to stdout/stderr with functions not listed 
  276.    in win/wtext.h */
  277.  
  278. #undef kbhit
  279. #undef getche
  280. #undef getch
  281. #undef putch
  282.  
  283. #undef fgetc
  284. #undef getchar
  285. #undef getc
  286. #undef fgets
  287. #undef gets
  288.  
  289. #undef fputc
  290. #undef putchar
  291. #undef putc
  292. #undef fputs
  293. #undef puts
  294.  
  295. #undef fprintf
  296. #undef printf
  297. #undef vprintf
  298. #undef vfprintf
  299.  
  300. #undef fwrite
  301. #undef fread
  302.  
  303. #if defined(__MSC__)|| defined(WIN32)
  304. #define isterm(f) (f==stdin || f==stdout || f==stderr)
  305. #else
  306. #define isterm(f) isatty(fileno(f))
  307. #endif
  308.  
  309. int
  310. MyPutCh(int ch)
  311. {
  312.     return TextPutCh(&textwin, (BYTE)ch);
  313. }
  314.  
  315. int
  316. MyKBHit(void)
  317. {
  318.     return TextKBHit(&textwin);
  319. }
  320.  
  321. int
  322. MyGetCh(void)
  323. {
  324.     return TextGetCh(&textwin);
  325. }
  326.  
  327. int
  328. MyGetChE(void)
  329. {
  330.     return TextGetChE(&textwin);
  331. }
  332.  
  333. int
  334. MyFGetC(FILE *file)
  335. {
  336.     if (isterm(file)) {
  337.         return MyGetChE();
  338.     }
  339.     return fgetc(file);
  340. }
  341.  
  342. char *
  343. MyGetS(char *str)
  344. {
  345.     TextPutS(&textwin,"\nDANGER: gets() used\n");
  346.     MyFGetS(str,80,stdin);
  347.     if (strlen(str) > 0 
  348.      && str[strlen(str)-1]=='\n')
  349.         str[strlen(str)-1] = '\0';
  350.     return str;
  351. }
  352.  
  353. char *
  354. MyFGetS(char *str, unsigned int size, FILE *file)
  355. {
  356. char FAR *p;
  357.     if (isterm(file)) {
  358.         p = TextGetS(&textwin, str, size);
  359.         if (p != (char FAR *)NULL)
  360.             return str;
  361.         return (char *)NULL;
  362.     }    
  363.     return fgets(str,size,file);
  364. }
  365.  
  366. int
  367. MyFPutC(int ch, FILE *file)
  368. {
  369.     if (isterm(file)) {
  370.         MyPutCh((BYTE)ch);
  371.         TextMessage();
  372.         return ch;
  373.     }
  374.     return fputc(ch,file);
  375. }
  376.  
  377. int
  378. MyFPutS(char *str, FILE *file)
  379. {
  380.     if (isterm(file)) {
  381.         TextPutS(&textwin, str);
  382.         TextMessage();
  383.         return (*str);    /* different from Borland library */
  384.     }
  385.     return fputs(str,file);
  386. }
  387.  
  388. int
  389. MyPutS(char *str)
  390. {
  391.     TextPutS(&textwin, str);
  392.     MyPutCh('\n');
  393.     TextMessage();
  394.     return 0;    /* different from Borland library */
  395. }
  396.  
  397. int MyFPrintF(FILE *file, char *fmt, ...)
  398. {
  399. int count;
  400. va_list args;
  401.     va_start(args,fmt);
  402.     if (isterm(file)) {
  403.         char buf[MAXPRINTF];
  404.         count = vsprintf(buf,fmt,args);
  405.         TextPutS(&textwin,buf);
  406.     }
  407.     else
  408.         count = vfprintf(file, fmt, args);
  409.     va_end(args);
  410.     return count;
  411. }
  412.  
  413. int MyPrintF(char *fmt, ...)
  414. {
  415. int count;
  416. char buf[MAXPRINTF];
  417. va_list args;
  418.     va_start(args,fmt);
  419.     count = vsprintf(buf,fmt,args);
  420.     TextPutS(&textwin,buf);
  421.     va_end(args);
  422.     return count;
  423. }
  424.  
  425. size_t MyFWrite(const void *ptr, size_t size, size_t n, FILE *file)
  426. {
  427.     if (isterm(file)) {
  428.         size_t i;
  429.         for (i=0; i<n; i++)
  430.             TextPutCh(&textwin, ((BYTE *)ptr)[i]);
  431.         TextMessage();
  432.         return n;
  433.     }
  434.     return fwrite(ptr, size, n, file);
  435. }
  436.  
  437. size_t MyFRead(void *ptr, size_t size, size_t n, FILE *file)
  438. {
  439.     if (isterm(file)) {
  440.         size_t i;
  441.         for (i=0; i<n; i++)
  442.             ((BYTE *)ptr)[i] = TextGetChE(&textwin);
  443.         TextMessage();
  444.         return n;
  445.     }
  446.     return fread(ptr, size, n, file);
  447. }
  448.  
  449. /* public interface to printer routines : Windows PRN emulation
  450.  * (formerly in win.trm)
  451.  */
  452.  
  453. #define MAX_PRT_LEN 256
  454. static char win_prntmp[MAX_PRT_LEN+1];
  455.  
  456. FILE *
  457. open_printer()
  458. {
  459. char *temp;
  460.     if ((temp = getenv("TEMP")) == (char *)NULL)
  461.         *win_prntmp='\0';
  462.     else  {
  463.         strncpy(win_prntmp,temp,MAX_PRT_LEN);
  464.         /* stop X's in path being converted by mktemp */
  465.         for (temp=win_prntmp; *temp; temp++)
  466.             *temp = tolower(*temp);
  467.         if ( strlen(win_prntmp) && (win_prntmp[strlen(win_prntmp)-1]!='\\') )
  468.             strcat(win_prntmp,"\\");
  469.     }
  470.     strncat(win_prntmp, "_gptmp",MAX_PRT_LEN-strlen(win_prntmp));
  471.     strncat(win_prntmp, "XXXXXX",MAX_PRT_LEN-strlen(win_prntmp));
  472.     mktemp(win_prntmp);
  473.     return fopen(win_prntmp, "w");
  474. }
  475.  
  476. void
  477. close_printer(FILE *outfile)
  478. {
  479.     fclose(outfile);
  480.     DumpPrinter(graphwin.hWndGraph, graphwin.Title, win_prntmp);
  481. }
  482.  
  483. void
  484. screen_dump(void)
  485. {
  486.     GraphPrint(&graphwin);
  487. }
  488.  
  489.  
  490.